package com.ntv.presentation.spring.web.backoffice;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ntv.domain.IUser;
import com.ntv.domain.Login;
import com.ntv.domain.News;
import com.ntv.domain.User;
import com.ntv.service.DomainException;
import com.ntv.service.news.INewsAppService;

@Controller
@RequestMapping("/upload_news.html")
@SessionAttributes("userSession")
public class NewsEntryFormController {

	private static final Logger logger = Logger
	.getLogger(NewsEntryFormController.class);

	private static final String NEWS_FORM = "news_entry_form";
	private static final String MA_LOGIN= "login";

	@Autowired
	private INewsAppService newsAppService;

	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView newForm(Model argModle, 
			@ModelAttribute(MA_LOGIN) Login  login, HttpServletRequest request){
		ModelAndView mNv = null;
		IUser user = (IUser)request.getSession().getAttribute("userSession");
		if (user == null) {
			login.setTarget("upload_news.html");
			return new ModelAndView("signin");
		}

		try {
			argModle.addAttribute(NEWS_FORM, new News());
			mNv = new ModelAndView("admin.news.entry","newstypes",
					newsAppService.getNewsTypes());
		} catch (DomainException e) {
			e.printStackTrace();
			logger.error(e);
			mNv = new ModelAndView("/backoffice.html");
		}
		return mNv;

	}

	/**
	 * Processes - New "NEWS" registration request.
	 * @param news - News POJO object.
	 * @param result - For error handling.
	 * @return The name of landing screen.
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String sumbitForm(
			@ModelAttribute(NEWS_FORM) News news,
			@RequestParam("multipartFile") MultipartFile multipartFile,
			@ModelAttribute("userSession") User user){
		final String onError = "redirect:/upload_news.html";
		final String onSuccess = "redirect:/backoffice.html";
		try
		{
			InputStream inStrm = multipartFile.getInputStream();
			long userID = user.getId();
			Date modifiedDate = new Date();

			news.setCreatedBy(userID);
			news.setUpdatedBy(userID);
			news.setCreatedOn(modifiedDate);
			news.setUpdatedOn(modifiedDate);


			if(inStrm == null){
				news.setHasContent(false);
			}else
			{
				news.setImage(multipartFile.getInputStream());
				news.setHasContent(true);
			}

			newsAppService.create(news);
			logger.info("News is successfully updated in NammaTV DB...");
			return onSuccess;
		}
		catch (DomainException e)
		{
			logger.error(e);
			e.printStackTrace();
			return onError;
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
			return onError;
		}
	}
}//:~

